package hangzhou;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import org.hibernate.*;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class loginservlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      String username = req.getParameter("uname");
      String password = req.getParameter("pswd");
      userDAO ud= new userDAO();
      resp.setContentType("text/html;charset=UTF-8");
      SessionFactory sf;
      HttpSession hs = req.getSession();
      sf = new Configuration().configure().buildSessionFactory();
      Session ss = sf.openSession();
      denglu dl = new denglu();
      hs.setAttribute("us", username);
      dl.setUsername(username);
      dl.setPassword(password);
      ss.save(dl);
      ss.beginTransaction().commit(); 
      ss.close();
      if(username.equals("root") && password.equals("123.com")) {
//    	  List<denglu> list = ud.getAllUsers();
//    	  req.setAttribute("list", list);
//    	  System.out.println("执行到了。。");
    	  //验证成功，跳转到分页功能的servlet。web.xml里的url-pattern值。
    	  req.setAttribute("currPage","1");
    	  req.getRequestDispatcher("pageQuery").forward(req, resp);
    	  //resp.sendRedirect("kongzhitai.jsp");
      }else {
    	   resp.sendRedirect("hangzhoumeijing.jsp");
      }
     
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	doGet(req, resp);
    }
}